package core.models;

import java.util.ArrayList;

import java.util.List;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.io.Serializable;
/**
 * A group of people (maximum of 5) for use in Oregon Trail.
 *
 * @author 4c1a
 */
public class Group implements Serializable
{
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private List<Person> people;
    private String name;
    private int money;
    private int distTravel;
    private Professions profession;
    private Rations ration;
    private Wagon wagon;
    private Places place;
    private Road road;
    
    
    
    /**
     * Creates a group of no more than 5 people.
     *
     * @param name The name of the group.
     * @param profession The profession of the group leader.
     * @param foodRations The starting food rations.
     * @param person The people to be in the group.
     */
    public Group(String name, Professions profession, Wagon wagon, Rations ration, Person... person)
    {
    	this(name, profession, ration, wagon);
   
        for (Person p : person)
        {
            people.add(p);
        }		
        distTravel = 0;
        road = new Road();
    }

    /**
     * Creates a new group with a given name, profession, and food rations.
     *
     * @param name The name of the group.
     * @param profession The profession of the group leader.
     * @param foodRations The starting food rations of the group. 
     * * @param wagon The wagon to be assigned to the group.
     */
    public Group(String name, Professions profession, Rations ration, Wagon wagon) 
    {
        people = new ArrayList<Person>();
    	money = 1500;
        this.name = name;
        this.profession = profession;
        this.ration = ration;
        this.wagon = wagon;
        distTravel = 0;
        road = new Road();
    }
    
    /**
     * Creates default wagon with default money, and ration settings. Also initializes distance traveled to zero and creates player's group.
     * 
     * @param wagon The wagon to be assigned to the group.
     */
    public Group(Wagon wagon) {
    	people = new ArrayList<Person>();
    	this.wagon = wagon;
    	money = 1500;
    	distTravel = 0;
    	ration = Rations.MODERATE;
    	road = new Road();
    }

    /**
     * Returns a list of the people in the group.
     *
     * @return The people in the group.
     */
    public List<Person> getPeople()
    {
        return people;
    }

    /**
     * Sets people in group.
     *
     * @param group The new group.
     */
    public void setPeople(List<Person> people)
    {
        this.people = people;
    }
    
    public int getNumPeopleAlive()
    {
    	int peeps = 0;
    	for (int x = 0; x < 5; x++)
    	{
    		if(people.get(x).getAlive())
    		{
    			peeps++;
    		}
    	}
    	return peeps;
    }
    

    /**
     * Sets the main profession for the group.
     *
     * @param profession The new profession.
     */
    public void setProfession(Professions profession) 
    {
    	this.profession = profession;
    }

    /**
     * Returns the main profession of the group.
     *
     * @return The profession of the group.
     */
    public Professions getProfession() 
    {
    	return profession;
    }

    public Wagon getWagon() {
    	return wagon;
    }
    /**
     * Sets the name of the group. 
     *
     * @param name The new name of the group.
     */
    public void setName(String name)
    {
        this.name = name;
    }

    /**
     * Returns the name of the group.
     *
     * @return The name of the group.
     */
    public String getName()
    {
        return name;
    }
    public void moveWagon()
    {
    	if(getNumPeopleAlive() != 0)
    	{
    	distTravel += wagon.getPace().getSpeed() * 3;
    	boolean hasEaten = wagon.updateFood(ration.getConsumption());
    	if (!hasEaten)
    	{
    		JFrame y = new JFrame();
			JOptionPane.showMessageDialog(y, "You do not have enough Food!");
    	}
    	}
    	else
    	{
    		JOptionPane.showMessageDialog(null,"You cannot move because everyone is dead!");
    	}
    }

    /**
     * Returns the current funds of the group.
     *
     * @return The group's current funds. 
     */
    public int getMoney()
    {
    	return money;
    }
    
    /**
     * Returns the remaining food rations.
     *
     * @return The remaining food rations.
     */
    public Rations getRation()
    {
    	return ration;
    }

    /**
     * Sets food rations.
     *
     * @param foodRations The new food rations.
     */
    public void setRation(Rations ration)
    {
    	this.ration=ration;
    }

    /**
     * Adds a person to the group.
     *
     * @param p The new person to add.
     */
    public void addPerson(Person p)
    {
    	people.add(p);
    }

    /**
     * Sets the current funds of the group.
     *
     *
     * @param money The new funds of the group. 
     */
    public void setMoney(int money)
    {
    	this.money = money;
    }
    
    public void updateMoney() {
    	money = profession.getMoney();
    }
    /**
     * returns the distance traveled
     * 
     * @return double the distance traveled
     */
    public int getDistance()
    {
    	return distTravel;
    }
    
    /**
     * Increments travel by a specified number of units
     * 
     * @param distance the amount to travel
     */
    public void travel(int distance)
    {
    	distTravel += distance;
    }
    
    /**
     * Returns a string that prints the group.
     *
     * @return A string representing the group.
     */
    public String toString() 
    {
    	String groupToString = "";
    	
    	groupToString="Name: " + name + "\n";
    	groupToString+="Profession: " + profession + "\n";
    	groupToString+="Food Rations: " + ration + "\n";
    	int i = 1;
    	for (Person p : people)
    	{
    		groupToString+="Person " + i + " Name: " + p.getName() + "\n";
    		i++;
    	}
    	
    	groupToString+="Money: " + Integer.toString(money) + "\n";
    	
    	return groupToString;
    	
    }
    
    /**
     * 
     * @return place
     */
    public Places getPlace()
    {
    	return place;
    }
    
    /**
     * 
     * @param place
     * sets place to place
     */
    public void setPlace(Places place)
    {
    	this.place = place;
    }
    
    public Road getRoad()
    {
    	return road;
    }
    
    /**
     * 
     * @return distance to next store
     */
    public int toNextStore()
    {
    	if (distTravel <= 100)
    	{
    		return 100 - distTravel;
    	}
    	else 
    	{
    		return 300 - (distTravel % 300);
    	}
    	
    }
    
 
}
